
package sysAuth

import grails.converters.JSON
import initConfig.SysInitData
import sysUtil.JwtToken
import sysUtil.PageUtil
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysAuthSQL
import sysUtil.SysAuthUtil
import sysUtil.SysCommUtil

import java.text.SimpleDateFormat

class LoginAuthController {

    /**
     *
     * @param userId
     * @param passWord
     * @return
     * @description :此处只用验证用户是否存在即可，如果不存在则返回null
     *
     * SESSION_CURRENT_USER:设置
     */


    def authUtilsService

    def login() {

        def result = ResultJson.resultJson()

        def userNo = String.valueOf(params?.userNo)
        def passWord = String.valueOf(params?.passWord)
        try {
            SysUsers user = SysUsers.findByUserNo(userNo.trim().toLowerCase())
            if (user) {
                if (user.passWord.equals(passWord.encodeAsMD5())) {
                    def userGrade = authUtilsService.isAvailable(user)
                    user.setLoginTime(new Date())
                    user.save(flush: true)
                    def jump = new HashMap() {
                        {
                            put("url", "index.html");
                            put("token", JwtToken.create(user.userNo,user.userName));
                            put("userName",user.userName)
                            put("userNo",user.userNo)
                            put("userGrade",userGrade)
                        }
                    }

                    result.put(ResultJson.DATA.name, jump)

                } else {
                    result.put(ResultJson.CODE.name, ResultCode.PWD_ERROR.code)
                    result.put(ResultJson.MESSAGE.name, ResultCode.PWD_ERROR.message)
                }
            } else {
                result.put(ResultJson.CODE.name, ResultCode.USER_NOT_EXIST.code)
                result.put(ResultJson.MESSAGE.name, ResultCode.USER_NOT_EXIST.message)
            }
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MESSAGE.name, ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        } finally {
            render result.toString()
        }
    }


    def logoff() {
        def result = ResultJson.resultJson()
       // prinltn params
        def u = session.getAttribute(SysInitData.SESSION_CURRENT_USER)
        //print "session.getAttribute(SysInitData.SESSION_CURRENT_USER) ${u}"
        if (u) {
            u = SysUsers.get(((SysUsers) u).id)
            if(u){
                u.setLogoffTime(new Date())
                u.save(flush: true)
            }
            session.removeAttribute(SysInitData.SESSION_CURRENT_USER)
            session.invalidate()
        }
        result.put(ResultJson.DATA.name,[url:'login.html'])
        render result.toString()
    }

    /**
     * 获取用户菜单权限
     * @return
     */
    def getUserFuncJSONList() {
        def result = ResultJson.resultJson()
        try {

            def user = SysUsers.findByUserNo(params.userNo)

            def list = authUtilsService.listUserTreeFuncByParent(user)
//            println list
            result.put(ResultJson.DATA.name, list)
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MESSAGE.name, ResultCode.TABLE_DATA_SUCCESS.message)

        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MESSAGE.name, ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        } finally {
            //println(  result.toString() )
            render result.toString()
        }
    }



}